package com.douxg.fastCoding.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douxg.fastCoding.db.Config;
import com.douxg.fastCoding.db.DbFunction;
import com.douxg.fastCoding.spring.vo.Result;
import com.douxg.fastCoding.web.consts.DbType;
import com.douxg.fastCoding.web.dto.DbInfoDto;
import com.douxg.fastCoding.web.entity.DbInfo;
import com.douxg.fastCoding.web.service.DbInfoService;
import com.douxg.fastCoding.web.vo.DbInfoVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/7/22 0:45
 */
@RequestMapping("fc/dbInfo")
@RestController
public class DbInfoController {
    @Resource
    private DbInfoService service;

    @PostMapping("add")
    public Result add(@RequestBody DbInfo dbInfo) {
        DbType dbType = DbType.getByName(dbInfo.getDbType());
        DbFunction dbFunction = dbType.getDbFunction();
        dbFunction.handleJdbcUrl(dbInfo);
        LocalDateTime now = LocalDateTime.now();
        dbInfo.setUpdateTime(now);
        dbInfo.setCreateTime(now);
        boolean handleResult = service.save(dbInfo);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("modify")
    public Result modify(@RequestBody DbInfoDto dbInfo) {
        DbType dbType = DbType.getByName(dbInfo.getDbType());
        DbFunction dbFunction = dbType.getDbFunction();
        dbFunction.handleJdbcUrl(dbInfo);
        LocalDateTime now = LocalDateTime.now();
        dbInfo.setCreateTime(null);
        dbInfo.setUpdateTime(now);
        boolean handleResult = service.updateById(dbInfo);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("delete")
    public Result delete(@RequestBody DbInfoDto dbInfoDto) {
        boolean handleResult = service.removeById(dbInfoDto.getId());
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("page")
    public Result page(@RequestBody DbInfoDto dbInfoDto, Integer current, Integer size) {
        IPage<DbInfoVo> projectPage = service.pageWithProject(dbInfoDto, current, size);
        return Result.success(projectPage);
    }

    @PostMapping("config")
    public Result config(@RequestBody Config config, Integer id) {
        LocalDateTime now = LocalDateTime.now();
        DbInfo dbInfo = new DbInfo();
        dbInfo.setId(id);
        dbInfo.setUpdateTime(now);
        String configStr = JSON.toJSONString(config);
        dbInfo.setConfig(configStr);

        boolean handleResult = service.updateById(dbInfo);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }
}
